/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.stockkeeper.stock.module;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.letmeshare.stockkeeper.stock.entities.Item;

/**
 *
 * @author ibisuser1
 */
public class ItemModule {
    
    private EntityManager em;
    
    public static ItemModule getInstance(EntityManager em) {
        if (em == null) {
            return null;
        }
        return new ItemModule(em);
    }
    
    private ItemModule(EntityManager em) {
        this.em = em;
    }
    
    public void createItem(Item item) {
        if (item == null) {
            throw new IllegalArgumentException("Item required");
        }
        item.validate();
        em.persist(item);
    }
    
    public void updateItem (Item item) {
        if (item == null) {
            throw new IllegalArgumentException("Item required");
        }
        Item oldItem = getItemById(item.getId()); 
        oldItem.setCategory(item.getCategory());
        oldItem.setName(item.getName());
         oldItem.setUnitPrice(item.getUnitPrice());
         oldItem.setExclusiveTax(item.getExclusiveTax());
         oldItem.setObsolete(item.isObsolete());
        em.merge(oldItem);
    }
    
    public void deleteItem (Item item) {
        if (item == null) {
            throw new IllegalArgumentException("Item required");
        }
        item.setObsolete(true);
        em.merge(item);
    }
    
    public Item getItemById (String id) {
        if (id == null) {
            throw new IllegalArgumentException("Id required");
        }
        return em.find(Item.class, id);
    }
    
    public Item getItemByCode (String code) {
        Query query = em.createQuery("SELECT item FROM Item item WHERE item.code = :code AND item.obsolete=false");
        query.setParameter("code", code);
        List<Item> items = query.getResultList();
        return items.isEmpty() ? null : (Item)items.get(0);
    }
    
    public List<Item> getItemsByName (String name) {
        Query query = em.createQuery("SELECT item FROM Item item WHERE item.name = :name AND item.obsolete=false");
        query.setParameter("name", name);
        return query.getResultList();
    }
    
    public List<Item> getItemsByCategory (String catCode) {
        Query query = em.createQuery("SELECT item FROM Item item WHERE item.category.code = :catCode AND item.obsolete=false");
        query.setParameter("catCode", catCode);
        return query.getResultList();
    }
    
    public List<Item> lookUpItemsByNameOrCategory (String searchQuery) {
        Query query = em.createQuery("SELECT item FROM Item item "
                + "WHERE item.name LIKE %:searchQuery% OR item.category.name LIKE %:searchQuery% AND item.obsolete=false");
        query.setParameter("searchQuery", searchQuery);
        return query.getResultList();
    }
    
    public List<Item> getAllItems() {
        Query query = em.createQuery("SELECT item FROM Item item");
        return query.getResultList();
    }
}
